package com.flute.search.bean;

import java.util.ArrayList;
import java.util.List;

import com.flute.search.util.MyXppDriver;
import com.flute.search.util.XmlObjectUtil;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;
import com.thoughtworks.xstream.converters.basic.DateConverter;

@XStreamAlias("searcher_alias")
public class Searcher {

	@XStreamAsAttribute
	private List<Search> searcher;

	public static Searcher fromXml(String xml) {
		XStream xstream = new XStream(new MyXppDriver());
		xstream.processAnnotations(Searcher.class);
		xstream.registerConverter(new DateConverter());
		return (Searcher) xstream.fromXML(xml);
	}

	public boolean compare(String alias) {
		if (searcher == null) {
			return false;
		}
		for (Search s : searcher) {
			if (s.getAlias() != null && s.getAlias().equals(alias)) {
				return true;
			}
		}
		return false;
	}

	public List<Search> getSearcher(String[] searcherArray) {
		List<Search> result = new ArrayList<Search>();
		if (searcher == null) {
			return result;
		}
		for (Search search : searcher) {
			for (String s : searcherArray) {
				if (search.getValue() != null && search.getValue().equals(s)) {
					result.add(search);
				}
			}
		}
		return result;
	}

	public String getAlias(String selectSearcher) {
		if (searcher == null) {
			return null;
		}
		for (Search search : searcher) {
			if (search.getValue() != null
					&& search.getValue().equals(selectSearcher)) {
				return search.getAlias();
			}
		}
		return null;
	}

	public List<Search> getSearcher() {
		return searcher;
	}

	public void setSearcher(List<Search> searcher) {
		this.searcher = searcher;
	}

	public static void main(String[] args) {
		Searcher s = new Searcher();
		Search a = new Search();
		a.setName("dd");
		a.setValue("vv");
		a.setAlias("ss");
		List<Search> ss = new ArrayList<Search>();
		ss.add(a);
		s.setSearcher(ss);
		System.out.println(XmlObjectUtil.toXml(s));
	}
}
